// pages/promotionDetail/index.js
const app = getApp();
const utils = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    cardId: 0,
    cardInfo: {},
    promotionList: [],
    jjkInfo: {},
    timeList: []
  },

  openMap: function (e) {
    let latitude = e.currentTarget.dataset.latitude;
    let longitude = e.currentTarget.dataset.longitude;
    let company = e.currentTarget.dataset.company;
    wx.showModal({
      title: '提示',
      content: '是否打开地图导航',
      success: function (res) {
        if (res.confirm) {
          wx.openLocation({
            latitude: latitude,
            longitude: longitude,
            scale: 28,
            name: "点击右边打开地图导航",
            address: company
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.cardId) {
      this.getInfo(options.cardId);
    }
  },

  getInfo: function (cardId) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Staff/GetInfo?id=' + cardId,
      success: function (res) {
        if (res.data.Code == 0) {
          if (res.data.Data) {
            that.getPromotionList(res.data.Data.Uid);
            that.getJJKinfo(res.data.Data.Uid);
            that.getShip(res.data.Data.Uid);
            let info = res.data.Data;
            let cardInfo = {};
            let avater = utils.formatAvater(info.Avater, 'https://static.711688.net.cn/upload/card/source/');
            cardInfo.company = info.Company;
            cardInfo.slider = avater;
            cardInfo.Latitude = info.Latitude;
            cardInfo.Longitude = info.Longitude;
            cardInfo.address = info.ProvinceName + info.CityName + info.AreaName + info.Address;
            that.setData({
              cardInfo: cardInfo
            })
          }
        }
      }
    })
  },

  getPromotionList: function (uid) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Promotion/GetDiscountList?uid=' + uid + '&showCount=1',
      success: function (res) {
        if (res.data.Code == 0) {
          let list = [];
          if (res.data.Data.Items.length > 0) {
            list = res.data.Data.Items;
            for (let i = 0; i < list.length; i++) {
              list[i].StartTime = list[i].StartTime.substr(0, 10);
              list[i].EndTime = list[i].EndTime.substr(0, 10);
              list[i].weekList = [];
              let weeks = list[i].Week;
              let weekList = [];
              if (weeks) {
                weeks = weeks.split(',');
                for (let j = 0; j < weeks.length; j++) {
                  if (weeks[j] == 1) {
                    weekList.push('星期一');
                  } else if (weeks[j] == 2) {
                    weekList.push('星期二');
                  } else if (weeks[j] == 3) {
                    weekList.push('星期三');
                  } else if (weeks[j] == 4) {
                    weekList.push('星期四');
                  } else if (weeks[j] == 5) {
                    weekList.push('星期五');
                  } else if (weeks[j] == 6) {
                    weekList.push('星期六');
                  } else if (weeks[j] == 0) {
                    weekList.push('星期日');
                  }
                }
                list[i].weekList = weekList;
              }
              list[i].Discount = 100 - list[i].UseDiscount;
              if (list[i].Discount.length > 5) {
                list[i].Discount = parseFloat(list[i].Discount).toFixed(2)
              }
              list[i].ShowDiscount = list[i].Discount / 10;
              list[i].Lave = parseInt(list[i].Stock - list[i].OrderCount)
            }
            that.setData({
              promotionList: list
            })
          }
        }
      }
    })
  },
  getJJKinfo: function (uid) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Coupon/GetCardConfigList?createUid=' + uid,
      success: function (res) {
        if (res.data.Code == 0) {
          if (res.data.Data && res.data.Data.Items.length > 0) {
            let list = res.data.Data.Items;
            let jjkInfo = '';
            for (let i = 0; i < list.length; i++) {
              if (list[i].CardType == 4) {//玖玖卡
                list[i].ShowDiscount = (100 - list[i].UseDiscount) / 10;
                jjkInfo = list[i];
              }
              break;
            }
            that.setData({
              jjkInfo: jjkInfo
            })
          }
        }
      }
    })
  },
  getShip: function (uid) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Ship/GetShip?uid=' + uid,
      success: function (res) {
        if (res.data.Code == 0) {
          if (res.data.Data) {
            if (res.data.Data.WorkTime) {
              let timeList = JSON.parse(res.data.Data.WorkTime);
              that.setData({
                timeList: timeList
              })
            }
          }
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})